package com.gulimall.member.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.gulimall.common.exception.BizCondeEnume;
import com.gulimall.member.excetion.PhoneExistException;
import com.gulimall.member.excetion.UsernameExistExcetion;
import com.gulimall.member.feign.CouponFeignService;
import com.gulimall.common.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.gulimall.member.entity.MemberEntity;
import com.gulimall.member.service.MemberService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.gulimall.member.feign.CouponFeignService;
import com.gulimall.common.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.gulimall.member.entity.MemberEntity;
import com.gulimall.member.service.MemberService;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.R;
import com.gulimall.common.vo.SocialUser;
import com.gulimall.common.vo.UserLoginVo;
import com.gulimall.member.entity.MemberEntity;
import com.gulimall.member.excetion.PhoneExistException;
import com.gulimall.member.excetion.UsernameExistExcetion;
import com.gulimall.member.feign.CouponFeignService;
import com.gulimall.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import javax.swing.*;

/**
 * 会员
 *
 * @author zk
 * @email zk@gmail.com
 * @date 2020-11-04 08:23:00
 */
@RestController
@RequestMapping("/member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        R membercoupons = couponFeignService.membercoupons();
        PageUtils page = memberService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 登录
     */
    @PostMapping("login")
    public R login(@RequestBody UserLoginVo vo) {
        MemberEntity entity = memberService.login(vo);
        if (entity != null) {
            return R.ok().setData(entity);
        } else {
            return R.error(BizCondeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(), BizCondeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
        //  return R.ok();
    }

    /**
     * 会员注册
     */
    @PostMapping("/regist")
    public R regist(@RequestBody UserLoginVo vo) {
        try {
            memberService.regist(vo);
        } catch (PhoneExistException e) {
            return R.error(BizCondeEnume.PHONE_EXIST_EXCEPTION.getCode(), BizCondeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UsernameExistExcetion e) {
            return R.error(BizCondeEnume.USER_EXIST_EXCEPTION.getCode(), BizCondeEnume.USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 社交登录
     */
    @PostMapping("/oauth2/login")
    public R oauthlogin(@RequestBody SocialUser socialUser) throws Exception {
        MemberEntity entity = memberService.login(socialUser);
        if (entity != null) {
            //登录成功
            return R.ok().setData(entity);
        } else {
            return R.error(BizCondeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(), BizCondeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
        //  return R.ok();
    }

}
